﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AD.Web.Common;
using AD.Contract;
using AD.Contract.Models;
namespace AD.Web.Areas.BaseInfo.Controllers
{
    public class CategoryController : ADController
    {
        //
        // GET: /BaseInfo/Category/

        public ActionResult Index(CategoryRequest requst,string CategoryType )
        {
            var result = this.CategoryService.GetCategoryList(CategoryType, requst);
            ViewBag.CategoryType = CategoryType;
            return View(result);
        }
        //测试
        private ActionResult Add()
        {
            BaseCategory c = new BaseCategory { CategoryType =ECategoryType.BusinessType.ToString(), Comment = "广告业务类型", CreateTime = DateTime.Now, FlagUse = true, Id = Guid.NewGuid().ToString(), Name = "寿光一台", nID = 0, Sort = 0 };
            this.CategoryService.Save(c);
            return View();
        }
        public ActionResult Create(string CategoryType)
        {
            var model = new BaseCategory();
            model.CategoryType = CategoryType;
            return View("Edit", model);
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            var model = new BaseCategory(); 
         
            model.Sort = 0;
            model.FlagUse = true;
            model.Comment = "";
            this.TryUpdateModel<BaseCategory>(model);
           
            this.CategoryService.Save(model);

            return this.RefreshParent();
        }
        public ActionResult Edit(string  id)
        {
            var model = this.CategoryService.GetModel(id);
            return View(model);
        }

        //
        // POST: /OA/Branch/Edit/5

        [HttpPost]
        public ActionResult Edit(string  id, FormCollection collection)
        {
            var model = this.CategoryService.GetModel(id);
            this.TryUpdateModel<BaseCategory>(model);

            this.CategoryService.Save(model);

            return this.RefreshParent();
        }
        [HttpPost]
        public ActionResult Delete(List<string> ids)
        {
            var model = this.CategoryService.GetModel(ids.First());
            this.CategoryService.Delete(ids);
        
            return RedirectToAction("Index", new { CategoryType = model.CategoryType });


        }

    }
}
